package com.itheima.sh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.sh.exception.ExceptionCast;
import com.itheima.sh.mapper.UserMapper;
import com.itheima.sh.pojo.User;
import com.itheima.sh.response.ResponseResult;
import com.itheima.sh.response.StatusCode;
import com.itheima.sh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Dqh
 * @create 2020-09-17 21:05
 * @desc
 **/
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @Override
    public User add(User user) {
        if (user.getUserName()==null){
            ExceptionCast.cast(ResponseResult.errorResult(StatusCode.APUSER_USERNAMEISNULL_ERROR));
        }
        userMapper.add(user);
        return user;
    }

    /**
     * @param uuid
     * @desc: 根据uuid删除用户
     * @return:
     * @author: Dqh  2020/9/17  21:58
     */
    @Override
    public boolean deleteUser(Integer uuid) {
        return userMapper.deleteUser(uuid);
    }

    /**
     * 查询单个用户信息
     *
     * @param uuid
     * @return
     */
    @Override
    public User findOne(Integer uuid) {
        return userMapper.findOne(uuid);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @Override
    public boolean update(User user) {
        return userMapper.update(user);
    }

    /**
     * 查询全部用户信息带分页
     *
     * @param user
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<User> search(User user, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Page<User> pageList =
                (Page<User>) userMapper.search(user);
        return pageList;
    }


}
